# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=lxml
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=5.4.0
pkgrel=1
pkgdesc="Python binding for the libxml2 and libxslt libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'mingw32' 'ucrt64' 'clang64' 'clangarm64')
url="https://lxml.de/"
msys2_repository_url="https://github.com/lxml/lxml"
msys2_references=(
  'purl: pkg:pypi/lxml'
  "cpe: cpe:/a:lxml:lxml"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-python")
if [[ ${CARCH} != i686 ]]; then
optdepends=("${MINGW_PACKAGE_PREFIX}-python-beautifulsoup4: support for parsing not well formed HTML"
            "${MINGW_PACKAGE_PREFIX}-python-cssselect: support for cssselect"
            "${MINGW_PACKAGE_PREFIX}-python-html5lib: support for html5lib parser")
fi
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "use-distutils-get_platform.patch"
        "0001-setupinfo-fix-library-name.patch")
sha256sums=('d12832e1dbea4be280b22fd0ea7c9b87f0d8fc51ba06e92dc62d52f804f78ebd'
            '6e58bd3072a6a77bf50c4c203beef589c97873e9902d4c8c6151abe71035f186'
            '7d9e5380a8461e3c2bb4b8e0ece52011b26e3112b94b97e548c927524cfc93de')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"
  _apply_patch_with_msg \
    use-distutils-get_platform.patch \
    0001-setupinfo-fix-library-name.patch
}

build() {
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation \
    -C--build-option=--with-cython -C--build-option=--with-unicode-strings
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSES.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
  install -Dm644 doc/licenses/BSD.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/BSD.txt
  install -Dm644 doc/licenses/elementtree.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/elementtree.txt
}
